<template>
    <el-dialog
            :title="!dataForm.userId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="用户ID，主键" prop="userId">
                        <el-input v-model.number="dataForm.userId" placeholder="用户ID，主键"></el-input>
                </el-form-item>
                <el-form-item label="用户名，唯一标识" prop="username">
                        <el-input v-model="dataForm.username" placeholder="用户名，唯一标识"></el-input>
                </el-form-item>
                <el-form-item label="加密后的密码" prop="password">
                        <el-input v-model="dataForm.password" placeholder="加密后的密码"></el-input>
                </el-form-item>
                <el-form-item label="用户邮箱，可选，用于注册或找回密码" prop="email">
                        <el-input v-model="dataForm.email" placeholder="用户邮箱，可选，用于注册或找回密码"></el-input>
                </el-form-item>
                <el-form-item label="用户手机号，可选，用于注册或验证" prop="phone">
                        <el-input v-model="dataForm.phone" placeholder="用户手机号，可选，用于注册或验证"></el-input>
                </el-form-item>
                <el-form-item label="社交媒体账号ID，可选，用于第三方登录" prop="socialId">
                        <el-input v-model="dataForm.socialId" placeholder="社交媒体账号ID，可选，用于第三方登录"></el-input>
                </el-form-item>
                <el-form-item label="用户昵称" prop="nickname">
                        <el-input v-model="dataForm.nickname" placeholder="用户昵称"></el-input>
                </el-form-item>
                <el-form-item label="用户头像URL" prop="avatarUrl">
                        <el-input v-model="dataForm.avatarUrl" placeholder="用户头像URL"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="gender">
                        <el-input v-model="dataForm.gender" placeholder="性别"></el-input>
                </el-form-item>
                <el-form-item label="出生日期" prop="birthDate">
                        <el-date-picker
                                v-model="dataForm.birthDate"
                                type="datetime"
                                placeholder="出生日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="用户角色，默认为学生" prop="role">
                        <el-input v-model="dataForm.role" placeholder="用户角色，默认为学生"></el-input>
                </el-form-item>
                <el-form-item label="创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="更新时间">
                        </el-date-picker>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            userId: null,
            username: null,
            password: null,
            email: null,
            phone: null,
            socialId: null,
            nickname: null,
            avatarUrl: null,
            gender: null,
            birthDate: null,
            role: null,
            createdAt: null,
            updatedAt: null
          },
          dataRule: {
            userId: [
                        { required: true, message: '用户ID，主键不能为空', trigger: 'blur' }
            ],
            username: [
                        { required: true, message: '用户名，唯一标识不能为空', trigger: 'blur' }
            ],
            password: [
                        { required: true, message: '加密后的密码不能为空', trigger: 'blur' }
            ],
            email: [
                        { required: true, message: '用户邮箱，可选，用于注册或找回密码不能为空', trigger: 'blur' }
            ],
            phone: [
                        { required: true, message: '用户手机号，可选，用于注册或验证不能为空', trigger: 'blur' }
            ],
            socialId: [
                        { required: true, message: '社交媒体账号ID，可选，用于第三方登录不能为空', trigger: 'blur' }
            ],
            nickname: [
                        { required: true, message: '用户昵称不能为空', trigger: 'blur' }
            ],
            avatarUrl: [
                        { required: true, message: '用户头像URL不能为空', trigger: 'blur' }
            ],
            gender: [
                        { required: true, message: '性别不能为空', trigger: 'blur' }
            ],
            birthDate: [
                        { required: true, message: '出生日期不能为空', trigger: 'blur' }
            ],
            role: [
                        { required: true, message: '用户角色，默认为学生不能为空', trigger: 'blur' }
            ],
            createdAt: [
                        { required: true, message: '创建时间不能为空', trigger: 'blur' }
            ],
            updatedAt: [
                        { required: true, message: '更新时间不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.userId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.userId = id || 0
            if (this.dataForm.userId) {
              this.$http({
                url: this.$http.adornUrl(`/app/user/info/${this.dataForm.userId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appuser
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/user/${!this.dataForm.userId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>